$(function(){
	let page=1;
	let userName=$(".news-int>p").text();
	let file=window.location.pathname;
	let s=file.substr(file.lastIndexOf("-")+1);
	s=s.substr(0,s.lastIndexOf("."));
	
	
	
	let fn=function(s){
		if(s==1){
			return "社会新闻";
		}else if(s==2){
			return "国内新闻";
		}
		else if(s==3){
			return "国际新闻";
		}
		else if(s==4){
			return "军事新闻";
		}else{
			return "精品图片";
		}
	}
	/**
	 * 加载分页数据
	 * @param {Object} current
	 */
	let loadData = function(current){
		$.get(
			"news-index-show.do",
			{newsClassId:s},
			function(data){
				$(".news-index-show").text(fn(s));
				formatData(data.list);
				formatPage(data);
				
			},"json"
		);
	}
	let dataCommentDiv = $(".news-list-show-lan").remove();
	/**
	 * 格式化数据
	 * @param {Object} list
	 */
	let formatData = function(list){
		
		$.each(list,function(i,item){
			var newsContent = item.newsContent;
			var index=newsContent.lastIndexOf("src")
			var a = newsContent.substr(index + 5);
			a = a.substr(0, a.indexOf("\""));
			let div = dataCommentDiv.clone();
			$("._nextPage").before(div);
			div.find(".news-p1").text(item.newsWorker);
			div.find(".news-p2").text(item.newsTime);
		    div.find(".caption-href").text(item.newsTitle);
		    if(index==-1){
		    	div.find(".news-imgs").attr("src", "img/_noimg.png");
		    }
		    else if(a!=null && a != "" && a != undefined){
		    	div.find(".news-imgs").attr("src",a);
			}else{
		    	div.find(".news-imgs").attr("src", "img/_noimg.png");
			}
			div.find(".btn-primary").attr("href","news-show-"+item.newsId+".html");
			div.find(".caption-href").attr("href","news-show-"+item.newsId+".html");
		});
		
		
	}
	/**
	 * 格式化分页按钮
	 * @param {Object} p
	 */
	let formatPage= function(p){
		if(parseInt(p.total)>p.pageSize*page){
			$("._nextPage").attr("style","display:block");
			page++;
		}else{
			$("._nextPage").attr("style","display:none");
			$("._null").attr("style","display:block");
		}
	}
	loadData(page);
	/**
	 * 处理分页事件
	 */
	$("._nextPage").on("click",function(){
		loadData(page);
	});
	
})
